package com.zhongshan.ga.mobile.logic;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.restlet.representation.Representation;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zhongshan.ga.commonvar.ArgsConfig;
import com.zhongshan.ga.logger.GenerateLog;
import com.zhongshan.ga.mobile.tools.ServerCheckMethod;

/**
 * 逻辑处理基类
 * @author yanqi
 *
 */
public class ParentLogic {
	
	/**
	 * Logic父类,basicCheck方法用于处理基本问题，如Token失效，序列号不正确等
	 * @param acceptObject Android传输过来的JSON数据
	 * @param command 序列号类型
	 * @param isCheckToken isCheckToken 是否检验Token
	 * @return 返回Android的JSON数据
	 */
	public JsonObject basicCheck(JsonObject acceptJson, String command, Boolean isCheckToken) {
		JsonObject resultJson = new JsonObject();
		System.out.println("++++++++++++++++++++++++++++++++++++++++" + command);
		GenerateLog.outDebug("****************************ParentLogic is starting **********************");
		String operateCode = acceptJson.get(ArgsConfig.OPERATE_CODE).getAsString();
		GenerateLog.outDebug("******************************操作码是"+ operateCode + "************************");
		resultJson = acceptJson;
		GenerateLog.outDebug("结果：" + resultJson);
		/*resultJson.addProperty(ArgsConfig.OPERATE_CODE, operateCode);*/
		resultJson.addProperty(ArgsConfig.RESULT_CODE, ArgsConfig.RESPONSE_SUCCESS);
		GenerateLog.outDebug(resultJson);
		resultJson.addProperty(ArgsConfig.RESULT_DESC, "响应成功");
		GenerateLog.outDebug(resultJson);
		GenerateLog.outDebug(command.toString() + "=======================================");
		/**
		 * 判断客户所传的操作命令是否和请求接口的命令相同
		 */
		if(!operateCode.equals(command)) {
			GenerateLog.outDebug("*********************************OperateCode Type Error *********************");
			resultJson.addProperty(ArgsConfig.RESULT_CODE, ArgsConfig.RESPONSE_FAIL);
			resultJson.addProperty(ArgsConfig.RESULT_DESC, "请求类型错误");
			return resultJson;
		}
		/**
		 * 判断是否需要检验token
		 */
		if(!isCheckToken) {
			GenerateLog.outDebug("********************************* Don't Check Token *********************");
			return resultJson;
		}
		
		String token = acceptJson.get(ArgsConfig.TOKEN).toString();
		/**
		 * 判断token是否为空
		 */
		if(token == null) {
			GenerateLog.outDebug("********************************* Token is null **************************");
			resultJson.addProperty(ArgsConfig.RESULT_CODE, ArgsConfig.RESPONSE_FAIL);
			resultJson.addProperty(ArgsConfig.RESULT_DESC, "无验证信息，请重新登录");
			return resultJson;
		}
		
		/**
		 * 判断token的效时间
		 */
		if(!ServerCheckMethod.isLegalToken(token)) {
			GenerateLog.outDebug("********************************* Token is not legal **************************");
			resultJson.addProperty(ArgsConfig.RESULT_CODE, ArgsConfig.RESPONSE_FAIL);
			resultJson.addProperty(ArgsConfig.RESULT_DESC, "验证信息失效，请重新登录");
			GenerateLog.outDebug("**********************************回应的消息为"+ resultJson + "******************");
			return resultJson;
		}
		return resultJson;
	}
	
	/**
	 * 把json中页数参数提取出来
	 * @param dataJson
	 * @return
	 */
	protected Map<String, Integer> jsonIndexLimitMap(JsonObject dataJson) {
		int startIndex = 0;
		int endIndex = 10;
		if (dataJson.get(ArgsConfig.START_INDEX) != null) {
			startIndex = dataJson.get(ArgsConfig.START_INDEX).getAsInt();
		}
		if (dataJson.get(ArgsConfig.RECORD_NUM) != null) {
			endIndex = startIndex
					+ dataJson.get(ArgsConfig.RECORD_NUM).getAsInt();
		}
		Map<String, Integer> indexLimitMap = new HashMap<String, Integer>();
		indexLimitMap.put("startIndex", startIndex);
		indexLimitMap.put("endIndex", endIndex);
		return indexLimitMap;
	}
	
	/**
	 * 从请求中取出json数据
	 * @param representation
	 * @return
	 * @throws IOException
	 */
	protected JsonObject getJsonFromRequest(Representation representation) throws IOException {
		String requestText = representation.getText();
		JsonObject acceptJson = new JsonParser().parse(requestText)
				.getAsJsonObject();
		
		GenerateLog.outDebug(acceptJson + "-----");
		return acceptJson;
	}
	
}
